package app.com.itcast.biyesheji.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import app.com.itcast.biyesheji.Bean.UsercgBean;
import app.com.itcast.biyesheji.R;

public class DenluActivity extends AppCompatActivity {

    private EditText phone;
    private EditText password;
    private Button login;
    private TextView zhuce;
    private TextView back;
    private int userid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_denlu);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        phone = findViewById(R.id.et_user_name);
        password = findViewById(R.id.et_psw);
        login = findViewById(R.id.btn_login);
        zhuce = findViewById(R.id.tv_register);
        back = findViewById(R.id.tv_back);
    }

    private void initData() {
    }

    private void initEvent() {
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();

            }
        });
        zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DenluActivity.this,ZhuceActivity.class);
                startActivity(intent);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DenluActivity.this,WodeActivity.class);
                startActivity(intent);
            }
        });
    }

    private void getData() {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        String url = getResources().getString(R.string.api_ip) + "/denlu/momo/login";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("phone", phone.getText().toString());
            jsonObject.put("password", password.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("TAG", "onResponse: " + jsonObject);
                /*处理结果数据*/
                // 1）讲json对象转换为java对象（需要使用GsonFormat插件根据返回数据，创建结果处理类MyBean）
                Gson gson = new Gson();
//                UserBean myBean = gson.fromJson(jsonObject.toString(), UserBean.class);
                UsercgBean usercgBean =gson.fromJson(jsonObject.toString(),UsercgBean.class);
                if(usercgBean.isSuccess()){
                    Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
                    UsercgBean.DataBean dataBean = usercgBean.getData();

                    SharedPreferences userinfo = getSharedPreferences("data",MODE_PRIVATE);
                    SharedPreferences.Editor usid =userinfo.edit();
                    usid.putInt("userid",dataBean.getUserid());
                    usid.commit();
                    Intent intent = new Intent(DenluActivity.this,WoderightActivity.class);
//                    intent.putExtra("userid",dataBean.getUserid());
                    startActivity(intent);
                }else{
                    Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

}